<?php
/**
 * 数据库配置信息
 */
require_once APP_ROOT . '/Config/Db.php';
use Config\Db;
/**
 * 类名：DB
 * 说明：数据库操作类
 */
class LibDatabase
{
    public $host;            //服务器
    public $username;        //数据库用户名
    public $password;        //数据密码
    public $dbname;          //数据库名
    public $conn;            //数据库连接变量

    /**
     * DB类构造函数
     */
    public function __construct($host='' ,$username='',$password='',$dbname='')
    {
        $db = Db::$db1;
        if(empty($host)){ //使用默认配置的数据库
            $host = $db["host"];
            $username = $db["user"];
            $password = $db["password"];
            $dbname = $db["dbname"];
        }
        $this->host = $host;
        $this->username = $username;
        $this->password = $password;
        $this->dbname = $dbname;
    }
    /**
     * 打开数据库连接
     */
    public function open()
    {
        $this->conn = mysqli_connect($this->host,$this->username,$this->password);
        mysqli_select_db($this->conn,$this->dbname);
        mysqli_query($this->conn,"SET CHARACTER SET utf8");
    }
    /**
     * 关闭数据连接
     */
    public function close()
    {
        mysqli_close($this->conn);
    }

    public function insert($tableName,$params){
        try {
            $sql = 'insert into ' . $tableName . "( ";
            if (!is_array($params) || empty($params)) {
                return false;
            }
            $keys = array_keys($params);
            $values = array_values($params);
            $sql .= implode($keys, ',');
            $sql = rtrim($sql, ",");
            $sql .= ") values('";
            $sql .= implode($values, "','");
            $sql .= "')";
            $this->open();
            mysqli_query($this->conn, $sql);
            $this->close();
            return true;
        }catch (Exception $e){
            return false;
        }
    }

    /*
     * $fields为一个数组，里面包含要查询的字段，比如$fields = aarray('id','channel_id','user_name')
     * $params用于where条件，比如要实现where channel_id=1 and user_name='xiaoming'，则$params应该为 array("channel_id"=>1,"user_name"=>"xiaoming")
     *
     */
    public function select($tableName,$fields="*",$params){
        try{
            //$fields = array('id','channel_id','user_name');
            //判断$fileds是不是一个数组，如果不是数组，则为查询*
            if(!is_array($fields)){
                $fieldsStr = "*";
            }else{
                $fieldsStr = implode(",",$fields);
                $fieldsStr = rtrim($fieldsStr,',');
            }
            $sql = "select {$fieldsStr} from {$tableName} ";
            $where = "";
            if(is_array($params)){
                $where = " where true ";
                foreach($params as $key=>$value){
                    $where .= " and {$key}='{$value}' ";
                }
            }
            $sql .= $where;
            $this->open();
            $query = mysqli_query($this->conn, $sql);
            $fieldinfo = mysqli_fetch_fields($query);
            $result = array();
            $cnt = 0;
            while($rows = mysqli_fetch_array($query)){
                foreach($fieldinfo as $val){
                    $result[$cnt][$val->name] = $rows[$val->name];
                }
                $cnt++;
            }
            $this->close();
            return $result;
        }catch (Exception $e){
            return array();
        }
    }

    public function queryDataBySql($sql){
        try{
            $this->open();
            $query = mysqli_query($this->conn, $sql);
            $fieldinfo = mysqli_fetch_fields($query);
            $result = array();
            $cnt = 0;
            while($rows = mysqli_fetch_array($query)){
                foreach($fieldinfo as $val){
                    $result[$cnt][$val->name] = $rows[$val->name];
                }
                $cnt++;
            }
            $this->close();
            return $result;
        }catch (Exception $e){
            return array();
        }
    }

    public function updateBySql($sql){
        try{
            $this->open();
            mysqli_query($this->conn, $sql);
            $this->close();
            return true;
        }catch (Exception $e){
            return false;
        }
    }
}

